<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Session;
use think\Controller;

class History_model
{
	// 历史记录
	public function getindex()
	{
		
		$uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        //用户操作 上一条相同  不计入操作数据库
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '历史记录') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'History/index',   
                'log_desc' => '历史记录',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }
        
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();

        
        // 登录用户的历史操作  分页
        $record = Db::table('cn_user_log')
                ->where('user_id',$uid)
                ->field('log_desc,create_time,log_id')
                ->order('create_time desc')
                ->paginate(10);
        $page = $record->render();
        $info = [
                'list' => $list,
                'record' => $record,
                'page' => $page
            ];
       
        return $info;
      
	}
    
}

?>